package com.ignou.mca.evote;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ignou.mca.datasource.DataBaseUtilities;
import com.ignou.mca.datasource.Encryption;
import com.ignou.mca.beans.Message;
import com.ignou.mca.beans.PartiesFormBean;
import com.ignou.mca.beans.PositionsFormBean;
import com.ignou.mca.util.SessionUtil;

public class AddPositions extends HttpServlet implements Servlet {

	Connection con = null;
	DataBaseUtilities dbu = null;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String positionName = req.getParameter("positionName");
		String positionDescription = req.getParameter("positionDescription");

		Message message = new Message();
		PositionsFormBean posts = new PositionsFormBean();
		posts.setName(positionName);
		posts.setDescription(positionDescription);

		HttpSession session = req.getSession(true);
		session.setAttribute("Message", message);
		req.setAttribute("Positions", posts);
		ServletContext context = getServletContext();
		dbu = new DataBaseUtilities();

		if (!SessionUtil.validateSession(req, resp)) {
			resp.sendRedirect(req.getServletPath() + "/Login.jsp");
			return;
		}
		
		if ((positionName.equalsIgnoreCase(""))) {
			message.setMessage("You have not completed all the fields.Please complete all mandatory fields.");
			context.getRequestDispatcher("/AddPositions.jsp").forward(req, resp);
		} else {

			String query = "insert into Positions(Name,description) values(?,?)";

			try {
				if (con == null)
					con = dbu.getConnection();
				
				if(isPositionExists(positionName))
				{
					req.setAttribute("ErrorMessage", "Position Already added in database.");
					context.getRequestDispatcher("/TransactionFailed.jsp").forward(req, resp);
				}else{
					
					PreparedStatement pst = con.prepareStatement(query);
					pst.setString(1, posts.getName());
					pst.setString(2, posts.getDescription());

					int i = pst.executeUpdate();

					if (i == 1) {
						context.getRequestDispatcher("/AdminMenu.jsp").forward(req,resp);
					} else {
						req.setAttribute("ErrorMessage",
										"Unable to connect to database. Please try again later.");
						context.getRequestDispatcher("/TransactionFailed.jsp").forward(req, resp);
					}
				}
			} catch (Exception e) {

			}

		}
		
	}
	
	private boolean isPositionExists(String position)
	{
		if(con==null) con = dbu.getConnection();
		
		dbu = new DataBaseUtilities();
		
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from positions where Name='"+ position+"'");

			while (rs.next()) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}
}